/**
 * FileStore.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.store.services;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

import extjsdyntran.translation.ITranslationService;
import extjsdyntran.translation.store.AStore;

public class FileStore extends AStore {

	private String path ;
	private boolean warn1 = false;
	private boolean warn2 = false;
	
	public FileStore(HashMap<String,String> params) throws Exception {
		super(params);
		this.path = params.get("path");
	}


	public void loadLanguage(ITranslationService service, String language)throws Exception{

		File file = new File(path+ language+ ".properties");
		if (file.exists()) {
				logger.info("Loading translations for language {}", language);
				FileInputStream fis = new FileInputStream(file);
				Properties p = new Properties();
				p.load(fis);
				fis.close();
				service.appendTranslationToCache(language, new HashMap(p));
		}else{
			logger.warn("Language {} not found in store service!",language);
		}
	}
	

	private Properties getPropfromHashMap(Map<String,String> map){
		Properties prop = new Properties();
		Set<Entry<String,String>> set =map.entrySet();
		for(Entry<String,String> entry : set){
			prop.put(entry.getKey(), entry.getValue());
		}
		return prop;
	}
	
	public void saveLanguage(ITranslationService service, Map<String,String> p,String language)throws Exception{
		logger.info("Saving translations for language {}", language);
		FileOutputStream fos = new FileOutputStream(path + language + ".properties");
		Properties prop = getPropfromHashMap(p);
		prop.store(fos, "");
		fos.flush();
		fos.close();
		prop.clear();
		prop=null;
	}
	
	public boolean saveTranslation(String toLanguage, String fromValue, String toValue) {
		if(!warn2)
		logger.warn("For this store service method is not supported");
		warn2=true;
		return true;
	}



	public boolean deleteTranslation(String fromLanguage, String fromValue) {
		if(!warn1)
		logger.warn("For this store service method is not supported");
		warn1=true;
		return true;
	}



	public boolean clearTranslation(ITranslationService service, String language) {
		File file = new File(path+ language+ ".properties");
		return file.delete();
	}
	
	
	
}
